﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RgUtility.OrmForOracle.Tool
{
    public static class IEnumerableExtend
    {
        /// <summary>
        /// 将集合转换成指定字符分割的字符串。
        /// </summary>
        /// <typeparam name="T">集合类型。</typeparam>
        /// <param name="collection">集合。</param>
        /// <param name="delim">指定分割符。</param>
        /// <returns></returns>
        public static string ToCSV<T>(this IEnumerable<T> collection, string delim, bool addBrackets)
        {
            if (collection == null)
            {
                return "";
            }

            StringBuilder result = new StringBuilder();
            foreach (T value in collection)
            {
                if (addBrackets)
                {
                    //result.Append("[");
                    result.Append("\"");
                }
                result.Append(value);
                if (addBrackets)
                {
                    //result.Append("]");
                    result.Append("\"");
                }
                result.Append(delim);
            }
            if (result.Length > 0)
            {
                result.Length -= delim.Length;
            }
            return result.ToString();
        }
    }
}
